package cn.edu.zut.cs.service.impl;

import cn.edu.zut.cs.pojo.Equipment;
import cn.edu.zut.cs.mapper.EquipmentMapper;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.Weighing;
import cn.edu.zut.cs.service.EquipmentService;
import cn.edu.zut.cs.service.WeighingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备管理 服务实现类
 * </p>
 *
 * @author equipment
 * @since 2021-06-16
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Resource
    private EquipmentService equipmentService;
    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private WeighingService weighingService;

    //添加电子秤
    @Override
    public void addEquiment(Equipment equipment) {
        equipmentService.save(equipment);
    }
    //删除电子秤
    @Override
    public void deleteEquiment(String  equid) {
        equipmentMapper.deleteById(equid);
    }
    //修改电子称信息
    @Override
    public void updateEquiment(Equipment equipment) {
        equipmentMapper.updateById(equipment);
    }
    //根据id查询电子秤信息
    @Override
    public Equipment selectEquiment(User user,String equid) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        Equipment equipment;
        if(user.getUserlevel()==1){
            queryWrapper.eq("equid",equid).eq("equcompany",user.getUsercompany());
        }else{
            queryWrapper.eq("equid",equid).eq("equcompany",user.getUsercompany()).eq("equlocation",user.getUserlocation());
        }
        equipment = equipmentMapper.selectOne(queryWrapper);
        return equipment;
    }


    //查询所有设备信息
    @Override
    public List<Equipment> selectAllEquipment(User user) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("equcompany",user.getUsercompany());

        }else{
            queryWrapper.eq("equcompany",user.getUsercompany()).eq("equlocation",user.getUserlocation());
        }
        List<Equipment> equipment = equipmentMapper.selectList(queryWrapper);
        return equipment;
    }

    @Override
    public void countAllMoney(User user,Date stat,Date end) {
        Double weight = weighingService.countWeight(user);
        List<Equipment> equipment = equipmentService.selectAllEquipment(user);
        List<Weighing> weighings = weighingService.selectAllWeighting(user);
        for(Equipment equipment1:equipment){
            equipment1.setTotalweight(0.00);
            equipmentService.updateEquiment(equipment1);
        }
        for(Equipment equipment1:equipment){
            for (Weighing weighing1:weighings) {
                if(weighing1.getEquid().equals(equipment1.getEquid())){
                    if(((weighing1.getWeighttime().compareTo(stat))==0||(weighing1.getWeighttime().compareTo(stat)==1))&&(weighing1.getWeighttime().compareTo(end)==-1||weighing1.getWeighttime().compareTo(end)==0)){
                        equipment1.setTotalweight(equipment1.getTotalweight()+weighing1.getWeight());
                        equipment1.setWeightratio((equipment1.getWeightratio()+weighing1.getWeight())*1.0/weight);
                        equipmentService.updateEquiment(equipment1
                        );
                    }

                }
            }
        }
    }

    @Override
    public List<Equipment> UserExcelDownloads(User user) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("equcompany",user.getUsercompany());
        }else{
            queryWrapper.eq("equcompany",user.getUsercompany()).eq("equlocation",user.getUserlocation());
        }
        List<Equipment> equipment = equipmentMapper.selectList(queryWrapper);
        return equipment;
    }

    @Override
    public Equipment addE() {

        return null;
    }
}
